<!-- AppSideBar.vue -->
<template>
  <el-aside width="auto" class="sidebar" :style="{ width: collapseModel ? '64px' : '140px' }">
    <!-- 收起/展开按钮 -->
    <div class="toggle-btn" @click="collapseModel = !collapseModel">
      <el-icon :size="18">
        <component :is="collapseSync ? 'Expand' : 'Fold'" />
      </el-icon>
    </div>

    <!-- 菜单 -->
    <el-menu
      :collapse="collapseModel"
      :default-active="$route.path"
      router
      background-color="#304156"
      text-color="#bfcbd9"
      active-text-color="#409EFF"
    >
      <!-- 递归组件 -->
      <side-bar-item
        v-for="route in menuList"
        :key="route.path"
        :item="route"
      />
    </el-menu>
  </el-aside>
</template>

<script>
import SideBarItem from '@/components/SideBarItem.vue'
export default {
  name: 'AppSideBar',
  components: { SideBarItem },
  props: {
    /* 外部注入的菜单树 */
    menuList: {
      type: Array,
      required: true
    },
    /* 是否默认收起 */
    collapse: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      collapseSync: this.collapse
    }
  },
  computed: {
    collapseModel: {
      get() {
        return this.collapseSync
      },
      set(val) {
        this.collapseSync = val
        this.$emit('update:collapse', val) // 支持 .sync
      }
    }
  }
}
</script>

<script setup>
// import { Fold, Expand } from '@element-plus/icons-vue'
</script>

<style scoped>
.sidebar {
  height: 100vh;
  background: #304156;
  overflow: hidden;
  transition-duration: 0.3s;
}
.toggle-btn {
  height: 48px;
  line-height: 48px;
  color: #fff;
  text-align: center;
  cursor: pointer;
  border-bottom: 1px solid #263445;
}
.el-menu {
  border-right: none;
}
</style>